package com.zyd.blog.controller;

import com.zyd.blog.business.annotation.BussinessLog;
import com.zyd.blog.business.enums.PlatformEnum;
import com.zyd.blog.homeart.service.ICommentService;
import com.zyd.blog.homeart.service.ISoftProductService;
import com.zyd.blog.homeart.util.ResponseObject;
import com.zyd.blog.persistence.beans.CenterNew;
import com.zyd.blog.persistence.beans.DesignArticle;
import com.zyd.blog.persistence.beans.SoftNew;
import com.zyd.blog.persistence.beans.SoftProduct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
* <p>
    *  前端控制器
    * </p>
*
* @author liusongqi
* @since 2020-02-27
*/
@RestController
@RequestMapping("/softProduct")
public class SoftProductController {
    private final Logger logger = LoggerFactory.getLogger(SoftProductController.class);
    @Autowired
    private ISoftProductService iSoftProductService;
    @Autowired
    private ICommentService iCommentService;
    //软装产品 全部
    @RequestMapping(value = "/softAll", method = RequestMethod.POST)
    @BussinessLog(value = "/softProduct/softAll 软装产品 全部 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject soft(String userId,int page,int count) {
        ResponseObject ob = new ResponseObject();
        ob.setCode(0);
        ob.setData(iSoftProductService.soft(userId,page,count));
        return ob;
    }


    /**
     * 根据规则 变换 获取软装产品
     * region 0省  1 市  2区  3全国  4 全省  5 全市
     * sort    20 时间优先 21 点赞优先  22 热度优先
     * style 只要不为空 直接传过去 就可
     * province 省
     * city  市
     * area  区
     */
    @RequestMapping(value = "/sortAll", method = RequestMethod.POST)
    public ResponseObject sort(String region, String sort, String style, String province, String city, String area,String userId,String place,int page,int count) {
        logger.info("/softProduct/sortAll 软装产品 根据规则 region={},sort={},style={},province={},city={},area={},userId={},place={} ",region,sort,style,province,city,area,userId,place);
        ResponseObject ob = new ResponseObject();

        Map<String,Object> sort1;
        try {
            sort1 = iSoftProductService.sort(region, sort, style, province, city, area,userId,place,page,count);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(sort1);
        return ob;
    }


    /**
     *     根据文章 id  点赞   点赞数量加1
     * state 0收藏 1点赞 2评论 3分享
     */
    @RequestMapping(value = "/fabulous", method = RequestMethod.POST)
    public ResponseObject fabulous(String softId,String userId,String category,String state) {
        logger.info("/softProduct/fabulous 进行点赞 softId={},userId={},category={},state={} ",softId,userId,category,state);
        ResponseObject ob = new ResponseObject();
        Integer sort1;
        try {
            sort1 = iSoftProductService.fabulous(softId,userId,category,state);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(sort1);
        ob.setMsg("success");
        return ob;
    }


    /**
     *     根据文章 id  取消点赞   点赞数量减1
     * state 0收藏 1点赞 2评论 3分享
     */
    @RequestMapping(value = "/reduceZan", method = RequestMethod.POST)
    public ResponseObject reduceZan(String softId,String userId,String category,String state) {
        logger.info("/softProduct/reduceZan 取消点赞  softId={},userId={},category={},state={} ",softId,userId,category,state);
        ResponseObject ob = new ResponseObject();
        Integer integer;
        try {
            integer = iSoftProductService.reduceZan(softId,userId,category,state);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(integer);
        ob.setMsg("success");
        return ob;
    }




    /**
     *     推荐文章r  style  风格  place 场所
     *     先计算热度，若现在看的场所是别墅，就取别墅热度前10的数据。
     */
    @RequestMapping(value = "/recommendArticle", method = RequestMethod.POST)
    public ResponseObject recommendArticle(String style,String place) {
        logger.info("/softProduct/recommendArticle 推荐文章  style={},place={} ",style,place);
        ResponseObject ob = new ResponseObject();
        List<SoftProduct> softProducts;
        try {
            softProducts = iSoftProductService.recommendArticle(style, place);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(softProducts);
        ob.setMsg("success");
        return ob;
    }



    /**
     *     根据文章 id  获取具体内容
     */
    @RequestMapping(value = "/specific", method = RequestMethod.POST)
    public ResponseObject specific(String softId,String userId,String designStyle,String designPlace)  {
        logger.info("/softProduct/specific 获取具体内容, softId={},userId={},designStyle={},designPlace={}", softId, userId, designStyle, designPlace);
        ResponseObject ob = new ResponseObject();
        Map<String,Object> specific;
        try {
            specific = iSoftProductService.specific(softId, userId,designStyle,designPlace);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(specific);
        ob.setMsg("success");
        return ob;
    }

    /**
     *   我的参与
     *
     */
    @RequestMapping(value = "/myShare", method = RequestMethod.POST)
    public ResponseObject myShare(String userId,String state,String category,int page,int count)  {
        logger.info("/softProduct/myShare 我的参与, userId={},state={},category={}", userId, state, category);
        ResponseObject ob = new ResponseObject();
        Object specific;
        try {
            specific =  iSoftProductService.myShare(userId, state,category,page,count);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(specific);
        ob.setMsg("success");
        return ob;
    }

    // 在我的里面  批量删除  评论
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    public ResponseObject batchDelete(String designId,String commentId,String userId,String state)  {
        logger.info("/softProduct/batchDelete 批量删除 , userId={},state={},designId={},commentId={}", userId, state,designId,commentId);
        ResponseObject ob = new ResponseObject();
        int integer = 0;
        try {
            String[] split = commentId.split(",");
            for (int i = 0; i < split.length; i++) {
                iCommentService.deleteComment(designId, commentId, userId, state);
                integer = i;
            }
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(integer);
        ob.setMsg("success");
        return ob;
    }


    // 在我的里面  批量删除  分享
    @RequestMapping(value = "/deleteShare", method = RequestMethod.POST)
    public ResponseObject deleteShare(String id)  {
        logger.info("/softProduct/deleteShare 批量删除 分享 , id={}", id);
        ResponseObject ob = new ResponseObject();
        int integer = 0;
        try {
                iCommentService.deleteShare(id);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(integer);
        ob.setMsg("success");
        return ob;
    }


    //搜索
    @RequestMapping(value = "/searchSoft", method = RequestMethod.POST)
    @BussinessLog(value = "/softProduct/searchSoft 搜索 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject searchDesign(String value,String userId, int page, int count) {
        ResponseObject ob = new ResponseObject();
        Map<String,Object> integer;
        try {
            integer = iSoftProductService.searchDesign(value,userId,page,count);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(integer);
        ob.setMsg("success");
        return ob;
    }
}
